/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.escom.info.scanner.jsf;

import com.escom.administracion.dao.ServicioDAO;
import com.escom.administracionperfiles.hbm.dto.Servicio;
import com.escom.administracionperfiles.hbm.dto.ServicioId;
import com.escom.cliente.exception.DAOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.List;
import com.escom.info.scanner.jsf.util.JsfUtil;
import java.io.Serializable;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

/**
 *
 * @author yamil
 */
public class ServicioController implements Serializable{

    private ServicioDAO servicioDAO;
    private Servicio current;
    private List<Servicio> l;

    public Servicio getSelected() {
        if(current==null){
          current=new Servicio();
          current.setId(new ServicioId());
        }
        return current;
    }

    public void setCurrent(Servicio current) {
        this.current = current;
    }


    public ServicioDAO getServicioDAO() {
        return servicioDAO;
    }

    public void setServicioDAO(ServicioDAO servicioDAO) {
        this.servicioDAO = servicioDAO;
    }


    public List<Servicio> getListaServicios(){
        try {
            l=servicioDAO.findAll();
        } catch (DAOException ex) {
            Logger.getLogger(ServicioController.class.getName()).log(Level.SEVERE, null, ex);
        }
        return l;
    }

    public String prepareCreate(){
        current=new Servicio();
        current.setId(new ServicioId());
        return "Create";

    }
    public String prepareView(Servicio servicio){
        current=servicio;
        return "View";

    }
    public String prepareEdit(Servicio servicio){
        current=servicio;
        return "Edit";

    }
    public void prepareDestroy(Servicio servicio){
        try {
            servicioDAO.remove(servicio);
JsfUtil.addSuccessMessage("Servicio eliminado Satisfactoriamente");
        } catch (DAOException ex) {
            Logger.getLogger(ServicioController.class.getName()).log(Level.SEVERE, null, ex);
            JsfUtil.addErrorMessage("Error el servicio no ha sido eliminado"); //AQUI ESTA
        }
        //return "";
    }

    public String prepareList(){
        current=null;
        return "List";
        
    }

    public String create(){
        try {
            current.setDescripcion(current.getDescripcion());
            current.setDominio(current.getDominio());
           
            servicioDAO.create(current);
            JsfUtil.addSuccessMessage("Servicio creado Satisfactoriamente");
        } catch (DAOException ex) {
            Logger.getLogger(ServicioController.class.getName()).log(Level.SEVERE, null, ex);
            JsfUtil.addErrorMessage("Error el servicio no ha sido creado"); //AQUI ESTA
        }
        return "View";
    }
     public String update(){
        try {
            servicioDAO.edit(current);
        } catch (DAOException ex) {
            Logger.getLogger(ServicioController.class.getName()).log(Level.SEVERE, null, ex);
            JsfUtil.addErrorMessage("Error el servicio no ha sido actualizado"); //AQUI ESTA
        }
        return "View";
    }


      @FacesConverter(forClass=Servicio.class)
    public static class ServicioControllerConverter implements Converter {

        public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
           try{
                if (value == null || value.length() == 0) {
                    return null;
                }
                ServicioController controller = (ServicioController) facesContext.getApplication().getELResolver().
                getValue(facesContext.getELContext(), null, "servicioController");
                return controller.getServicioDAO().find(getKey(value));
           }catch(Exception e){
               e.printStackTrace();
           }
           return null;

        }

        java.lang.String getKey(String value) {
            java.lang.String key;
            key = value;
            return key;
        }

        String getStringKey(java.lang.String value) {
            StringBuffer sb = new StringBuffer();
            sb.append(value);
            return sb.toString();
        }

        public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
            if (object == null) {
                return null;
            }
            if (object instanceof Servicio) {
                Servicio o = (Servicio) object;
                return null;
            } else {
                throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: "+UsuariosController.class.getName());
            }
        }

    }

}
